package com.wen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wen.pojo.Cart;
import com.wen.pojo.dto.ProductAndCart;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xie
 */
public interface CartMapper extends BaseMapper<Cart> {


    @Select("SELECT * FROM `cart` c INNER JOIN  `product` p WHERE   c.`productId` = p.`id` AND p.`on_off` = 1 AND p.`STATUS` = \"C001-1\"  AND c.`consumer_id` = #{id}")
    List<ProductAndCart> selProductAndCart(String id);


    @Select("SELECT COUNT(*) FROM `cart` c INNER JOIN  `product` p WHERE   c.`productId` = p.`id`  AND c.`consumer_id` = #{id}")
    int selProductAndCartCount(String id);


    @Select("SELECT * FROM `cart` c INNER JOIN  `product` p WHERE   c.`productId` = p.`id` AND c.`selected` = 1 AND p.`on_off` = 1 AND p.`STATUS` = \"C001-1\"   AND  c.`consumer_id` = #{id}")
    List<ProductAndCart> selProductAndCartSelected(String id);

    @Select("SELECT SUM(c.count) AS `counts`,SUM(c.count * p.price) AS  sumprice FROM `cart` c INNER JOIN  `product` p WHERE   c.`productId` = p.`id` AND c.`selected` = 1 AND p.`on_off` = 1 AND p.`STATUS` = \"C001-1\"  AND  c.`consumer_id`= #{id}")
    ProductAndCart selProductAndCartSum(String id);


    @Update("UPDATE cart SET `count` = #{count} WHERE `productid` = #{pid}")
    int  UpdateCount  (@Param("count") String count,@Param("pid") Integer pid);

    @Update("UPDATE cart SET selected  = 0 WHERE `id` = #{id}")
    int UpdateSelected(String id);
}
